<?php

Class Captcha_model extends CI_Model {

    public function captcha() {
        $this->load->helper('captcha');
        $abc = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p");
        $word = "";
        $n = 0;
        while ($n < 6) {
            $word .= $abc[mt_rand(0, 25)];
            $n++;
        }

        $vals = array(
            'word' => (strtoupper($word)),
            'img_path' => './captcha/',
            'img_url' => base_url() . 'captcha/',
            'font_path' => './font/Sassy Stark.ttf',
            'img_width' => '250',
            'img_height' => 60,
            'expiration' => 1,
            'time' => time()
        );
        $cap = create_captcha($vals);
        //delete expire captcha
        $expire = $vals['time'];
        $this->db->where('time < ', $expire);
        $this->db->delete('captcha');

               $database = array(
            'time' => $cap['time'],
            'ip_address' => $this->input->ip_address(),
            'word' => $cap['word'],
        );
        $this->db->insert('captcha', $database);
        
        return $cap['image'];
    }

    public function login($u) {
       
        $this->db->select('*');
        $this->db->where('word', $u);
        $datalogin = $this->db->get('captcha');
        if ($datalogin->num_rows() > 0) {
         
            return TRUE;
        } else {
       
            return FALSE;
        }
    }

}

?>
